<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* loginHandler
*
* Encapsulates the Login Routines -- Must be instanced for obvious reasons
* 
*/

define("TRY_LOAD_ACCOUNT", 		0);
define("CREATE_ACCOUNT",   		1);
define("GET_EMAIL_ADDR",		2);
define("GET_PASSWORD_1ST",		3);
define("GET_PASSWORD_2ND",		4);
define("SHOW_USER_CHARACTERS", 	5);
define("CHARACTER_LOGIN",		6);
define("CHARACTER_VERIFY",		7);
define("LOGIN_RESUME",			8);

class loginMngr {
	public static $loginSessions = array();
	
	public static function show_welcome_screen($connector) {
		$connector->send("KUSHT",GAME_TITLE);
		$connector->send("");
		$connector->send("The MIT License &#169; ANDREW TUTT, MMX.",GAME_SUBTITLE);
		$connector->send("[[...and <em>many</em> others.|help copyright holders]]",GAME_COPYRIGHT);
		$connector->send("Give us a name &raquo;");
	}
}

class loginSession {
	public $connector;
	public $currentStep;

	public $proposedName;
	public $tmp_username;
	public $tmp_password;
	public $tmp_email;
	public $proposedCharName;

	public $useraccount;
	public $characters;

	function __construct($connector) {
		$this->connector = $connector;
		$this->currentStep = TRY_LOAD_ACCOUNT;
	}
	
	public function login($msg) {		
		switch ($this->currentStep) {
			case TRY_LOAD_ACCOUNT:		// They have not typed anything yet OR they've gone back to the beginning
				$this->try_load_account($msg);
				return;
			case CREATE_ACCOUNT:
				$this->create_new_account($msg);
				return;
			case GET_EMAIL_ADDR:
				$this->get_email_address($msg);
				return;
			case GET_PASSWORD_1ST:
				$this->get_password_first($msg);
				return;
			case GET_PASSWORD_2ND:
				$this->get_password_second($msg);
				return;
			case SHOW_USER_CHARACTERS:
				if($this->useraccount->password != $msg) {
					$this->connector->send("Wrong Password.");
					$this->connector->server->disconnect($this->connector->id,$this->connector->socket);
					return;
				}
				$this->show_user_characters();
				return;
			case CHARACTER_LOGIN:
				$this->character_login($msg);
				return;
			case CHARACTER_VERIFY:
				$this->character_verify($msg);
				return;
			case LOGIN_RESUME:
				if($this->useraccount->password != $msg) {
					$this->connector->send("Wrong Password.");
					$this->connector->server->disconnect($this->connector->id,$this->connector->socket);
					return;
				}
				$this->resume_play();
				return;
		}
	}
	
	public function try_load_account($msg) {
		if(!ctype_alnum($msg)) {
			$this->connector->send("Names must be alphanumeric. Try again. &raquo;");
			return;
		}
	
		$useraccount = dbHandler::loadUserAccount($msg);
		// Name not recognized -> Create a name
		if($useraccount===false) {
			if(!isset($this->proposedName)) { $this->proposedName = $msg; }
			$this->connector->send("That account isn't on file. 
			Type it again to create it, or type another name to try again. &raquo;");
			$this->currentStep = CREATE_ACCOUNT;
			return;
		} else {		
			if(get_obj_by_field_value($useraccount->id,"owner_id",World::$char_list)) {
				$this->connector->send("A character from that account is already connected and playing. 
					Enter your password to resume (WARNING: this will disconnect your other client) &raquo;");	
					$this->currentStep = LOGIN_RESUME;
					$this->useraccount = $useraccount;
					return;		
			} else {
				$this->useraccount = $useraccount;
				$this->connector->send("Password &raquo;");
				$this->currentStep = SHOW_USER_CHARACTERS;
				return;
			}
		}
	}
	
	public function create_new_account($msg) {
		if($this->proposedName == $msg) {
			$this->tmp_username = $msg;
			$this->connector->send("Alright " . $msg . " it is. Don't forget, it's case sensitive.");
			$this->connector->send("Let us know your email address? (no spam, we promise) &raquo;");
			$this->currentStep = GET_EMAIL_ADDR;
		} else {
			$this->currentStep = TRY_LOAD_ACCOUNT;
			$this->proposedName = $msg;
			$this->login($msg);
			return;
		}
	}
	
	public function get_email_address($msg) {
		$this->tmp_email = $msg;
		$this->connector->send("And now a password &raquo;");
		$this->currentStep = GET_PASSWORD_1ST;
		return;
	}
	
	public function get_password_first($msg) {
		$this->tmp_password = $msg;
		$this->connector->send("Retype it &raquo;");
		$this->currentStep = GET_PASSWORD_2ND;
		return;
	}
	
	public function get_password_second($msg) {
		if ($this->tmp_password == $msg) {
			dbHandler::saveUserAccount($this->tmp_username,$this->tmp_password,$this->tmp_email);
			$this->useraccount->id = $this->tmp_username;
			$this->connector->send("Got it. Congrats, you've got an account.");
			$this->connector->send("You don't have any character's yet! Type in a name to create a character. &raquo;");
			$this->currentStep = CHARACTER_LOGIN;
		} else {
			$this->connector->send("Your passwords don't match. Try again. &raquo;");
			$this->currentStep = GET_PASSWORD_1ST;
		}
	}
	
	public function show_user_characters() {		
		$characters = dbHandler::loadUserCharacters($this->useraccount->id);
		
		if($characters && $this->connector) {
			$this->characters = array();
			$this->connector->send("Your characters are:");
			foreach($characters as $char) {
				$tmp = new UserCharacter();
				$tmp->load($char['id']);
				$this->characters += array($tmp->copy_id=>$tmp);
				$this->connector->send($tmp->name,TEXT);
			}
			$this->connector->send("Login as one of your characters, or type in a new name to create a new one:");
			$this->currentStep = CHARACTER_LOGIN;
		} else {
			$this->connector->send("You don't have any character's yet! Type in a name to create a character. &raquo;");
			$this->currentStep = CHARACTER_LOGIN;
			return;
		}
	}
	
	public function character_login($msg) {
		$test = get_obj_by_field_value($msg,"name",$this->characters);
		if($test) {
			$this->move_to_game($test);
		// This forces unique usernames
		} elseif(dbHandler::charExists($msg)) {
			$this->connector->send("That character name already exists. Try another one instead.");
			$this->show_user_characters();	
		} else {
			$this->proposedCharName = $msg;
			$this->connector->send("You wish to be known as " . $msg . "? Is that right? (Yes / No) &raquo;");
			$this->currentStep = CHARACTER_VERIFY;
		}

	}
	
	public function move_to_game($char) {
		$char->load();
		$char->connector = $this->connector;
		$this->connector->user = $char;
		$char->init();

		// And load the client...
		$this->connector->user->loadClient();
		$this->connector->loggedin = true;
		
		// Delete from Memory
		$key = $this->connector->id;
		unset(loginMngr::$loginSessions[$key]);
		unset($this);
	}
	
	public function character_verify($msg) {
		if(stristr("yes",$msg)) { $this->create_character(); return; }
		else { $this->show_user_characters(); }
	}
	
	public function create_character() {
		$this->connector->user = new UserCharacter();
		$this->connector->user->connector = $this->connector;
		$this->connector->user->owner_id = $this->useraccount->id;
		
		// Give some basic information (temporary, this will eventually be made more sophisticated)
		$this->connector->user->name = $this->proposedCharName;
		$this->connector->user->description ="some guy";
		$this->connector->user->image ="woman1.jpg";
		$this->connector->user->thumb ="woman1_thumb.jpg";
		$this->connector->loggedin = true;
		$this->connector->user->setposition(STANDING);
		$this->connector->user->set_bit(FEMALE);
		
		// Now, save the character...
		$this->connector->user->save();	
		
		// Add to World and Room
		$startRoom = current(World::$room_list);
		World::add_char($this->connector->user);
		$startRoom->add_char($this->connector->user);
		
		// And load the client...
		$this->connector->user->loadClient();
		$this->connector->user->save();
			
		// Delete from Memory
		$key = $this->connector->id;
		unset(loginMngr::$loginSessions[$key]);
		unset($this);
	}

	public function resume_play() {
		$char = get_obj_by_field_value($this->useraccount->id,"owner_id",World::$char_list);
		if($char) {
			$cnxnToTerminate = $char->connector;
			$this->connector->user = $char;
			$char->connector = $this->connector;
			echo $cnxnToTerminate->socket . " connection overidden. Player " . 
			$char->name . " logged in through a different terminal as " . $char->connector->socket . "\n";
			$cnxnToTerminate->disconnect();
			$char->connector->loggedin = true;
			$char->send("Ok, you're logged in from here now.");
			$char->loadClient();
		} else {
			$this->show_user_characters();
		}
	}

}




?>
